package edu.ynu.se.xiecheng.achitectureclass.greenhouse.controller;

import edu.ynu.se.xiecheng.achitectureclass.common.controller.LogicController;
import edu.ynu.se.xiecheng.achitectureclass.greenhouse.dao.GreenhouseDAO;
import edu.ynu.se.xiecheng.achitectureclass.greenhouse.entity.Greenhouse;
import edu.ynu.se.xiecheng.achitectureclass.greenhouse.service.GreenhouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;

@Api(tags = "大棚信息管理")
@RestController
@RequestMapping("/greenhouse")
public class GreenhouseController extends LogicController<GreenhouseService, GreenhouseDAO, Greenhouse, Long> {

    private final GreenhouseService greenhouseService;

    @Autowired
    public GreenhouseController(GreenhouseService service) {
        super(service);
        this.greenhouseService = service;
    }

    /**
     * 获取所有大棚信息。
     *
     * @return 包含所有大棚的列表
     */
    @ApiOperation("获取所有大棚信息")
    @GetMapping("/all")
    public ResponseEntity<List<Greenhouse>> getAllGreenhouses() {
        List<Greenhouse> greenhouses = greenhouseService.getAllGreenhouses();
        return ResponseEntity.ok(greenhouses);
    }

    /**
     * 根据ID获取大棚信息。
     *
     * @param id 大棚的ID
     * @return 如果找到，返回大棚信息；否则返回404 Not Found
     */
    @ApiOperation("根据ID获取大棚信息")
    @GetMapping("/{id}")
    public ResponseEntity<Greenhouse> getGreenhouseById(@PathVariable Long id) {
        Optional<Greenhouse> greenhouse = greenhouseService.getGreenhouseById(id);
        return greenhouse.map(ResponseEntity::ok)
                .orElseGet(() -> ResponseEntity.notFound().build());
    }

    /**
     * 根据大棚编号获取大棚信息。
     *
     * @param number 大棚的唯一编号
     * @return 如果找到，返回大棚信息；否则返回404 Not Found
     */
    @ApiOperation("根据大棚编号获取大棚信息")
    @GetMapping("/number/{number}")
    public ResponseEntity<Greenhouse> getGreenhouseByNumber(@PathVariable String number) {
        Optional<Greenhouse> greenhouse = greenhouseService.getGreenhouseByNumber(number);
        return greenhouse.map(ResponseEntity::ok)
                .orElseGet(() -> ResponseEntity.notFound().build());
    }

    // 根据需要添加更多端点
}
